const API_BASE_URL = "https://wxsite.puruicloud.com/app/index.php?i=2&t=111111&c=entry&a=wxapp&m=video_vote&do=";
//const API_BASE_URL = "https://jackiezzh.cn/we7/app/index.php?i=2&t=111111&c=entry&a=wxapp&m=video_vote&do=";
const request = (url, method, data) =>{
  let _url = API_BASE_URL + url;
  return new Promise((resolve, reject)=>{
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(request) {
        resolve(request.data)
      },
      fail(error) {
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}

/**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}

module.exports = {
  request,
  getUserInfo: function(data){
    return request('UserLogin', 'get', data)
  },
  register: function (data) {
    return request('registerUser', 'post', data)
  },
  getBaseMineInfo:function (data){
    return request('getBaseMineInfo', 'get', data)
  },
  getActiveInfo:function(data){
    return request('getActiveInfo', 'get', data)
  },
  getVideoInfo: function(data){
    return request('getVideoInfo', 'get', data)
  },
  checkDoVote: function(data){
    return request('checkDoVote', 'post', data)
  },
  getOtherTimes: function(data){
    return request('getAnotherTimes', 'post', data)
  },
  getRankInfo: function(data){
    return request('getRankInfo', 'get', data)
  },
  getVideoList:function(data){
    return request('getVideoList', 'get', data)
  },
  getVoteActionList: function(data){
    return request('getVoteActionList', 'get', data)
  }
}